package com.twu.biblioteca.IOStream;

import com.twu.biblioteca.commands.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ConsoleIO implements IO {
    private InputStreamReader is = new InputStreamReader(System.in);
    private BufferedReader br = new BufferedReader(is);

    @Override
    public ICommand inputCommand() throws IOException {
//        InputStreamReader is = new InputStreamReader(System.in);
//        BufferedReader br = new BufferedReader(is);
        String currentOption = br.readLine();
        int commandNumber;
        if (currentOption != null)
            commandNumber = Integer.parseInt(currentOption.split(" ")[0]);
        else
            commandNumber = -1;
        ICommand command = null;

        switch(commandNumber) {
            case 1: {
                command = new ViewBooksCommand(commandNumber);
                break;
            }
            case 2: {
                if(currentOption.split(" ").length == 2) {
                    String bookId = currentOption.split(" ")[1];
                    command = new ReserveBooksCommand(commandNumber, bookId);
                }
                break;
            }
            case 3: {
                command = new CheckMemberDetailCommand(commandNumber);
                break;
            }
            case 4: {
                command = new ViewsMoviesCommand(commandNumber);
                break;
            }
            case 5: {
                if(currentOption.split(" ").length == 3) {
                    String username = currentOption.split(" ")[1];
                    String password= currentOption.split(" ")[2];
                    command = new LoginCommand(commandNumber, username, password);
                }
                break;
            }
            case 6: {
                command = new LogoutCommand(commandNumber);
                break;
            }
            case 7: {
                command = new QuitCommand(commandNumber);
                break;
            }
            default:
                break;
        }

        return command;
    }

    @Override
    public void outputCommand(Message message) {
        System.out.println(message.getMessage());
    }
}
